﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CXMLStudioIDE.Views;
using System.Windows.Forms;
using CXMLStudioIDE.DataAccessLayer;
using System.Web;
using System.Data;
using System.Collections;
using CXMLStudioIDE.Common.Entities;

namespace CXMLStudioIDE.Presenters
{
    public class DataDictionaryPresenter
    {
        #region Declarations
        private IDataDictionaryView view;
        #endregion

        #region Constructor
        public DataDictionaryPresenter(IDataDictionaryView view)
        {
            this.view = view;
            view.OnFormClosing += new FormClosingEventHandler(OnFormClosing);

        }
        #endregion

        #region Methods
        private void OnFormClosing(object sender, FormClosingEventArgs e)
        {
            view.Parent.CloseDataDictionary();
        }

        public void UpdateDataDictionaryView(BusinessEntity be)
        {
            view.EntityName = be.Name;

            DataDictionaryDAL dal = new DataDictionaryDAL();
            UserManagementDAL udal = new UserManagementDAL();

            view.ds_BE_datadictionary = dal.GetBusinessEntityDataDictionary(view.BusinessEntity.EntityType.EntityTypeContextID,view.BusinessEntity.EntityType.EntityTypeID);

            view.ClearEditors();

            if (view.ds_BE_datadictionary.Tables[0].Rows.Count > 0)
            {
                view.WriteEmtpyBDEditor();
                DataRow bdrow = view.ds_BE_datadictionary.Tables[0].Rows[0];
                string sZ_BusinessDef = bdrow["BusinessDefinition"].ToString();
                view.ContextualDefinitionID = (int)bdrow["ContextualDefinitionID"];
                view.BusinessDefinitionID = (int)bdrow["BusinessDefinitionID"];
                sZ_BusinessDef = sZ_BusinessDef.Replace("\n", "");
                sZ_BusinessDef = sZ_BusinessDef.Replace("\r", "");
                sZ_BusinessDef = sZ_BusinessDef.Replace("&lt;A href", "&lt;A target=&quot;_blank&quot; href");
                view.BusinessDefinition = HttpUtility.HtmlDecode(sZ_BusinessDef);
                //view.BDBodyHtml = HttpUtility.HtmlDecode(sZ_BusinessDef);

                foreach (System.Data.DataRow Row in view.ds_BE_datadictionary.Tables[2].Rows)
                {
                    view.AddKeyWord(Row.ItemArray[1].ToString());
                }
                string sZ_html = "";
                bool toggle_color = false;
                string color = "";
                foreach (System.Data.DataRow Row in view.ds_BE_datadictionary.Tables[1].Rows)
                {
                    color = (toggle_color ? "#CCFFE6" : "#CCFFCC");
                    toggle_color = (toggle_color ? false : true);
                    sZ_html += @"<div contentEditable='false' unselectable=""on"" style=""background-color:" + color + @""">";
                    sZ_html += @"<h5 unselectable=""on"">Date: " + Row.ItemArray[2];
                    sZ_html += @"&nbsp;&nbsp;By: " + udal.GetCXMLUserNameById((int)Row.ItemArray[3]) + @"</h5>";
                    sZ_html += Row.ItemArray[1];
                    view.WriteEmptyHCEditor();
                    sZ_html = sZ_html.Replace("\n", "");
                    sZ_html = sZ_html.Replace("\r", "");
                    sZ_html = sZ_html.Replace("&lt;A href", "&lt;A target=&quot;_blank&quot; href");
                    view.HistoricComments = HttpUtility.HtmlDecode(sZ_html);
                    sZ_html += @"</div>";
                }
            }
            else
            {
                view.Parent.Update();
            }
        }

        public bool SaveBusinessDefinition()
        {
            if (view.BusinessEntity.EntityType.EntityTypeContextID != 0 && view.BusinessEntity.EntityType.EntityTypeID != 0)
            {
                DataDictionaryDAL dal = new DataDictionaryDAL();
                UserManagementDAL udal = new UserManagementDAL();
                int i_ContextualDefinitionID = -1, i_BusinessDefinitionID = -1;
                if (view.BusinessDefBodyText != null && view.BusinessDefBodyText.Length > 0)
                {
                    view.RemoveEmptyBDLinks();
                    string sZ_EncodedBusinessDef = HttpUtility.HtmlEncode(view.BusinessDefinition);
                    DataSet ds = dal.SaveBusinessEntityDefinitionInContext(view.BusinessEntity.EntityType.EntityTypeContextID, view.BusinessEntity.EntityType.EntityTypeID, sZ_EncodedBusinessDef);
                    System.Data.DataRow Row = ds.Tables[0].Rows[0];
                    i_BusinessDefinitionID = (int)Row.ItemArray[0];
                    i_ContextualDefinitionID = (int)Row.ItemArray[1];
                    view.BusinessDefBodyText = "";
                    UpdateDataDictionaryView(view.BusinessEntity);
                    view.BDSaveEnabled = false;
                    return true;
                }
            }
            return false;
        }

        public bool SaveComment()
        {
            if (view.BusinessEntity.EntityType.EntityTypeContextID != 0 && view.BusinessEntity.EntityType.EntityTypeID != 0)
            {
                DataDictionaryDAL dal = new DataDictionaryDAL();
                UserManagementDAL udal = new UserManagementDAL();
                if (view.CommentBodyText != null && view.CommentBodyText.Length > 0)
                {
                    view.RemoveEmptyCommentLinks();
                    dal.SaveComment(HttpUtility.HtmlEncode(view.Comments), udal.GetCXMLUserIdByName(view.Parent.m_CXMLUser), view.ContextualDefinitionID, view.BusinessDefinitionID, -1);
                    view.CommentBodyText = "";
                    UpdateDataDictionaryView(view.BusinessEntity);
                    view.CommentSaveEnabled = false;
                    return true;
                }
            }
            return false;
        }
        #endregion
    }
}
